import { Injectable } from '@nestjs/common';
import { CreatePlaygroundDto } from './dto/create-playground.dto';
import { UpdatePlaygroundDto } from './dto/update-playground.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Playground } from './entities/playground.entity';

@Injectable()
export class PlaygroundService {
  constructor(
    @InjectRepository(Playground)
    private repository: Repository<Playground>,
  ) {}
  async create(createPlaygroundDto: CreatePlaygroundDto) {
    const entity = this.repository.create(createPlaygroundDto);
    return await this.repository.save(entity);
  }

  async findAll() {
    return await this.repository.find({
      order: {
        createTime: 'DESC',
      },
    });
  }

  async findOne(id: number) {
    const res = await this.repository.findOne({ where: { id } });
    return res;
  }

  async update(id: number, updatePlaygroundDto: UpdatePlaygroundDto) {
    await this.repository.update(id, updatePlaygroundDto);
    return await this.repository.find({ where: { id } });
  }

  async remove(id: number) {
    return await this.repository.delete(id);
  }
}
